package com.manage.project.biz.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.manage.project.biz.domain.BizSuidao;
import com.manage.project.biz.domain.Bizjidianpingdingfenxiang;
import com.manage.project.biz.service.IBizSuidaoService;
import com.manage.project.biz.service.IBizjidianpingdingfenxiangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.project.biz.domain.BizJishupingding;
import com.manage.project.biz.service.IBizJishupingdingService;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.web.page.TableDataInfo;

/**
 * 技术评定Controller
 *
 * @author liutao
 * @date 2022-05-03
 */
@RestController
@RequestMapping("/biz/jishupingding")
public class BizJishupingdingController extends BaseController
{
    @Autowired
    private IBizJishupingdingService bizJishupingdingService;
    @Autowired
    private IBizjidianpingdingfenxiangService bizjidianpingdingfenxiangService;
    @Autowired
    private IBizSuidaoService bizSuidaoService;

    /**
     * 查询技术评定列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:jishupingding:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizJishupingding bizJishupingding)
    {
        startPage();
        List<BizJishupingding> list = bizJishupingdingService.selectBizJishupingdingList(bizJishupingding);
        for (BizJishupingding jishupingding : list) {
            BizSuidao bizSuidao = bizSuidaoService.selectBizSuidaoById(Long.getLong(jishupingding.getField1()));
            if(bizSuidao!=null){
                jishupingding.setField1(bizSuidao.getField1());
                jishupingding.setField2(bizSuidao.getField2());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出技术评定列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:jishupingding:export')")
    @Log(title = "技术评定", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizJishupingding bizJishupingding)
    {
        List<BizJishupingding> list = bizJishupingdingService.selectBizJishupingdingList(bizJishupingding);
        ExcelUtil<BizJishupingding> util = new ExcelUtil<BizJishupingding>(BizJishupingding.class);
        return util.exportExcel(list, "技术评定数据");
    }

    /**
     * 获取技术评定详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:jishupingding:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizJishupingdingService.selectBizJishupingdingById(id));
    }

    /**
     * 新增技术评定
     */
//    @PreAuthorize("@ss.hasPermi('biz:jishupingding:add')")
    @Log(title = "技术评定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizJishupingding bizJishupingding){
        String suidaoName = bizJishupingding.getField2();
        // 获取隧道编号
        BizSuidao bizSuidao = new BizSuidao();
        bizSuidao.setField2(suidaoName);
        List<BizSuidao> listBizsuidao = bizSuidaoService.selectBizSuidaoList(bizSuidao);
        if (listBizsuidao != null){
            bizJishupingding.setField1(listBizsuidao.get(0).getField1());
        }
        BizJishupingding bizJishupingding1 = new BizJishupingding();
        List<BizJishupingding> listBizJSPD = bizJishupingdingService.selectBizJishupingdingList(bizJishupingding1);
        // 获取上次评定等级及日期
        int flag = 0; // 有无上次评定记录
        for (int i=0; i<listBizJSPD.size(); i++){
            if (suidaoName.equals(listBizJSPD.get(i).getField2())){
                flag = 1;
                bizJishupingding.setField4(listBizJSPD.get(i).getField4());
                bizJishupingding.setField5(listBizJSPD.get(i).getField6());
                break;
            }
        }
        if (flag == 0){
            bizJishupingding.setField4("无");
            bizJishupingding.setField5("无");
        }
        // 本次评定日期
        SimpleDateFormat fo = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        bizJishupingding.setField6(fo.format(today));
        // 计算JDCI
        Bizjidianpingdingfenxiang bizjidianpingdingfenxiang = new Bizjidianpingdingfenxiang();
        bizjidianpingdingfenxiang.setTunnelName(suidaoName);
        List<Bizjidianpingdingfenxiang> listPDFX = bizjidianpingdingfenxiangService.selectBizjidianpingdingfenxiangList(bizjidianpingdingfenxiang);
        int num1 = 0;// 供配电设施 数量
        int num2 = 0;// 照明设施 数量
        int num3 = 0;// 通风设施 数量
        int num4 = 0;// 消防设施 数量
        int num5 = 0;// 监控与通信设施 数量
        int avg1 = 0;// 供配电设施 平均设备完好率
        int avg2 = 0;// 照明设施 平均设备完好率
        int avg3 = 0;// 通风设施 平均设备完好率
        int avg4 = 0;// 消防设施 平均设备完好率
        int avg5 = 0;// 监控与通信设施 平均设备完好率
        for (int i=0; i<listPDFX.size(); i++){
            int shebeitypeid = listPDFX.get(i).getElectricalEquipmentId() / 10000;
            switch (shebeitypeid){
                case 1: num1++;
                        avg1+=listPDFX.get(i).getEquipmentCompleteness();
                        break;
                case 2: num2++;
                        avg2+=listPDFX.get(i).getEquipmentCompleteness();
                        break;
                case 3: num3++;
                        avg3+=listPDFX.get(i).getEquipmentCompleteness();
                        break;
                case 4: num4++;
                        avg4+=listPDFX.get(i).getEquipmentCompleteness();
                        break;
                case 5: num5++;
                        avg5+=listPDFX.get(i).getEquipmentCompleteness();
                        break;
            }
//            if ((listPDFX.get(i).getElectricalEquipmentId() / 10000) == 1){
//                num1++;
//            }
        }
        if (num1==0){
            avg1=0;
        } else {
            avg1/=num1;
        }
        if (num2==0){
            avg2=0;
        } else {
            avg2/=num2;
        }
        if (num3==0){
            avg3=0;
        } else {
            avg3/=num3;
        }
        if (num4==0){
            avg4=0;
        } else {
            avg4/=num4;
        }
        if (num5==0){
            avg5=0;
        } else {
            avg5/=num5;
        }
        int JDCI = ((avg1*23+avg2*18+avg3*19+avg4*21+avg5*19) / 100 ) * 100;
        JDCI/=100;
        bizJishupingding.setField7(String.valueOf(JDCI));
        // 计算评定等级
        if (JDCI>=97){
            bizJishupingding.setField8("1类");
        } else if (JDCI>=92 && JDCI<97){
            bizJishupingding.setField8("2类");
        }else if (JDCI>=84 && JDCI<92){
            bizJishupingding.setField8("3类");
        }else if (JDCI<84){
            bizJishupingding.setField8("4类");
        }
        // 供配电设施 状况值
        if (avg1>=98){
           bizJishupingding.setField10("0");
        } else if (avg1>=93 && avg1<98){
            bizJishupingding.setField10("1");
        } else if (avg1>=85 && avg1<93){
            bizJishupingding.setField10("2");
        } else if (avg1<85){
            bizJishupingding.setField10("3");
        }
        // 照明设施 状况值
        if (avg2>=95){
            bizJishupingding.setField11("0");
        } else if (avg2>=86 && avg2<95){
            bizJishupingding.setField11("1");
        } else if (avg2>=74 && avg2<86){
            bizJishupingding.setField11("2");
        } else if (avg2<74){
            bizJishupingding.setField11("3");
        }
        // 通风设施 状况值
        if (avg3>=98){
            bizJishupingding.setField12("0");
        } else if (avg3>=91 && avg3<98){
            bizJishupingding.setField12("1");
        } else if (avg3>=82 && avg3<91){
            bizJishupingding.setField12("2");
        } else if (avg3<82){
            bizJishupingding.setField12("3");
        }
        // 消防设施 状况值
        if (avg4>=100){
            bizJishupingding.setField13("0");
        } else if (avg4>=95 && avg4<100){
            bizJishupingding.setField13("1");
        } else if (avg4>=89 && avg4<95){
            bizJishupingding.setField13("2");
        } else if (avg4<89){
            bizJishupingding.setField13("3");
        }
        // 监控与通信设施 状况值
        if (avg5>=98){
            bizJishupingding.setField14("0");
        } else if (avg5>=91 && avg5<98){
            bizJishupingding.setField14("1");
        } else if (avg5>=81 && avg5<91){
            bizJishupingding.setField14("2");
        } else if (avg5<81){
            bizJishupingding.setField14("3");
        }
        // 养护措施建议
        int flag1 = Integer.parseInt(bizJishupingding.getField10());
        int flag2 = Integer.parseInt(bizJishupingding.getField11());
        int flag3 = Integer.parseInt(bizJishupingding.getField12());
        int flag4 = Integer.parseInt(bizJishupingding.getField13());
        int flag5 = Integer.parseInt(bizJishupingding.getField14());
        if (bizJishupingding.getField8().equals("1类")){
            bizJishupingding.setField9("1类机电设施应进行正常养护");
        }
        if (bizJishupingding.getField8().equals("2类") || flag1==1 || flag2==1 || flag3==1 || flag4==1 || flag5==1){
            bizJishupingding.setField9("2类机电设施或评定状况值为1的分项，应进行正常养护，并对损坏设备及时修复");
        }
        if (bizJishupingding.getField8().equals("3类") || flag1==2 || flag2==2 || flag3==2 || flag4==2 || flag5==2){
            bizJishupingding.setField9("3类机电设施或评定状况值为2的分项，宜实施专项工程，并应加强日常巡查。");
        }
        if (bizJishupingding.getField8().equals("4类") || flag1==3 || flag2==3 || flag3==3 || flag4==3 || flag5==3){
            bizJishupingding.setField9("4类机电设施或评定状况值为3的分项，应实施专项工程，并应加强日常巡查，并采取交通管制措施。");
        }

        return toAjax(bizJishupingdingService.insertBizJishupingding(bizJishupingding));
    }
    /**
    public AjaxResult add(@RequestBody BizJishupingding bizJishupingding)
    {
        BizJishupingding bizJishupingding2 = new BizJishupingding();
        // 获取当前表的所有记录
        List<BizJishupingding> list = bizJishupingdingService.selectBizJishupingdingList(bizJishupingding2);
        if(list.size()>0){// 当前表存在记录
            bizJishupingding.setField4(list.get(0).getField4());//上次评定等级
            bizJishupingding.setField5(DateUtils.dateTime(list.get(0).getCreateTime()));//上次创建时间作为本记录的上次评定日期
        }else{
            bizJishupingding.setField4("无");// 等级
            bizJishupingding.setField5("无");// 日期
        }
        //求得设备完好率
        List<Map<String,Object>> maps =  bizGongluMapper.getMaplist("SELECT (1-(zsb*gzs)/(zsb*ts)) whl FROM " +
                "(SELECT IFNULL((SELECT count(*) FROM biz_jidiansheshiguzhangxuncha GROUP BY field4 ),0) gzs)a ,(" +
                "SELECT count(*) gzt FROM biz_jidiansheshiguzhangxuncha) b,(SELECT count(*) zsb FROM biz_shebei) c," +
                "(SELECT (TO_DAYS(NOW()) - TO_DAYS((SELECT create_time FROM biz_shebei ORDER BY id asc  limit 1))) ts) d");
        String whl = maps.get(0).get("whl").toString();
        //求的所有故障设备类型
        List<Map<String,Object>> maps2 =  bizGongluMapper.getMaplist("SELECT field5 FROM biz_shebei WHERE field1 in " +
                "(SELECT field4 FROM biz_jidiansheshiguzhangxuncha GROUP BY field4)");
        //求得各设备得分和
        float l = 0f;
        //求得各设备权重之和
        float q = 0f;
        for (Map<String, Object> map : maps2) {
            if(map.get("field5").toString().equals("供配电设施")){
                q+=0.23;
                l+= (float) (MapUtils.getFloat(map,"field5")*0.23);
            }else if (map.get("field5").toString().equals("照明设施")){
                q+=0.18;
                l+= (float) (MapUtils.getFloat(map,"field5")*0.18);
            }else if (map.get("field5").toString().equals("通风设施")){
                q+=0.19;
                l+= (float) (MapUtils.getFloat(map,"field5")*0.19);
            }else if (map.get("field5").toString().equals("消费设施")){
                q+=0.21;
                l+= (float) (MapUtils.getFloat(map,"field5")*0.21);
            }else if (map.get("field5").toString().equals("监控与通信设施")){
                q+=0.19;
                l+= (float) (MapUtils.getFloat(map,"field5")*0.19);
            }
        }
        //求得jdci
        float jdci =  100*l/q;
        //求得类别
        String lev = "无";
        if(jdci>=97){ lev = "一类"; }else if(jdci>=92&& jdci<97){ lev = "二类";
        }else if(jdci>=84&& jdci<92){ lev = "三类";
        }else if(jdci<84){  lev = "四类"; }
        bizJishupingding.setField7(jdci+"");
        bizJishupingding.setField8(lev);
        bizJishupingding.setField6(DateUtils.getDate());
        return toAjax(bizJishupingdingService.insertBizJishupingding(bizJishupingding));
    }
     */

    /**
     * 修改技术评定
     */
    @Log(title = "技术评定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizJishupingding bizJishupingding)
    {
        return toAjax(bizJishupingdingService.updateBizJishupingding(bizJishupingding));
    }

    /**
     * 删除技术评定
     */
//    @PreAuthorize("@ss.hasPermi('biz:jishupingding:remove')")
    @Log(title = "技术评定", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizJishupingdingService.deleteBizJishupingdingByIds(ids));
    }
}
